package com.zch.starter.metrics.prometheus.springboot.starter.metrics;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 偏通用的指标对象，如果有个性化的业务指标对象可以继承该类
 */

public class AbstractMetricsInfo {
    public static final String DEFAULT = "DEFAULT";
    public static final String COUNTER = "COUNTER";
    public static final String TIMER = "TIMER";
    public static final String GAUGE = "GAUGE";

    public static final String COUNT_AND_TIME = "COUNT_AND_TIME";
    public static final String SUCCESS = "S";
    public static final String FAIL = "F";



    private String appid=DEFAULT;
    private String appScene=DEFAULT;
    private String type;
    private String metricsName = DEFAULT;
    private String description = DEFAULT;
    private long costTimeMs;
    private String metricsStatus;
    private String tag1=DEFAULT;
    private String tag2=DEFAULT;
    private String tag3=DEFAULT;
    private String tag4=DEFAULT;
    private String tag5=DEFAULT;
    private Map<String, String> tagMap = new ConcurrentHashMap<>();

    public AbstractMetricsInfo(){

    }
    public void putTag(String key,String value){
        if (StringUtils.isBlank(value)) {
            value = DEFAULT;
        }
        tagMap.put(key,value);
    }
    public static AbstractMetricsInfo _success(String name)  {
        return _success(name,DEFAULT);
    }
    public  static AbstractMetricsInfo _fail(String name)  {
        return _fail(name,DEFAULT);
    }
    public static AbstractMetricsInfo _time(String name, long costTimeMs)  {
        return _time(name,DEFAULT,costTimeMs);
    }
    public static AbstractMetricsInfo _countAndTime(String name, boolean success, long costTimeMs)  {
        return _countAndTime(name,DEFAULT,success,costTimeMs);
    }
    public static AbstractMetricsInfo _success(String name, String appid)  {
        AbstractMetricsInfo abstractMetricsInfo = new AbstractMetricsInfo();
        abstractMetricsInfo.setMetricsName(name);
        abstractMetricsInfo.setMetricsStatus(SUCCESS);
        abstractMetricsInfo.setType(COUNTER);
        abstractMetricsInfo.setAppid(StringUtils.isBlank(appid)?DEFAULT:appid);
        return abstractMetricsInfo;
    }
    public  static AbstractMetricsInfo _fail(String name, String appid)  {
        AbstractMetricsInfo abstractMetricsInfo = new AbstractMetricsInfo();
        abstractMetricsInfo.setMetricsName(name);
        abstractMetricsInfo.setMetricsStatus(FAIL);
        abstractMetricsInfo.setType(COUNTER);
        abstractMetricsInfo.setAppid(StringUtils.isBlank(appid)?DEFAULT:appid);
        return abstractMetricsInfo;
    }
    public static AbstractMetricsInfo _time(String name, String appid, long costTimeMs)  {
        AbstractMetricsInfo abstractMetricsInfo = new AbstractMetricsInfo();
        abstractMetricsInfo.setMetricsName(name);
        abstractMetricsInfo.setCostTimeMs(costTimeMs);
        abstractMetricsInfo.setType(TIMER);
        abstractMetricsInfo.setAppid(StringUtils.isBlank(appid)?DEFAULT:appid);
        return abstractMetricsInfo;
    }
    public static AbstractMetricsInfo _countAndTime(String name, String appid, boolean success, long costTimeMs)  {
        AbstractMetricsInfo abstractMetricsInfo = new AbstractMetricsInfo();
        abstractMetricsInfo.setMetricsName(name);
        abstractMetricsInfo.setCostTimeMs(costTimeMs);
        if(success){
            abstractMetricsInfo.setMetricsStatus(SUCCESS);
        }else{
            abstractMetricsInfo.setMetricsStatus(FAIL);
        }
        abstractMetricsInfo.setType(COUNT_AND_TIME);
        abstractMetricsInfo.setAppid(StringUtils.isBlank(appid)?DEFAULT:appid);
        return abstractMetricsInfo;
    }

    public Map<String,String> getTagMap() {
        return tagMap;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getMetricsName() {
        return metricsName;
    }

    public void setMetricsName(String metricsName) {
        this.metricsName = metricsName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public long getCostTimeMs() {
        return costTimeMs;
    }

    public void setCostTimeMs(long costTimeMs) {
        this.costTimeMs = costTimeMs;
    }

    public String getMetricsStatus() {
        return metricsStatus;
    }

    public void setMetricsStatus(String metricsStatus) {
        this.metricsStatus = metricsStatus;
        putTag("metricStatus",this.getMetricsStatus());
    }

    public String getTag1() {
        return tag1;
    }

    public void setTag1(String tag1) {
        this.tag1 = tag1;
        putTag("tag1",tag1);
    }

    public String getTag2() {
        return tag2;
    }

    public void setTag2(String tag2) {
        this.tag2 = tag2;
        putTag("tag2",tag2);
    }

    public String getTag3() {
        return tag3;
    }

    public void setTag3(String tag3) {
        this.tag3 = tag3;
        putTag("tag3",tag3);
    }

    public String getTag4() {
        return tag4;
    }

    public void setTag4(String tag4) {
        this.tag4 = tag4;
        putTag("tag4",tag4);

    }

    public String getTag5() {
        return tag5;
    }

    public void setTag5(String tag5) {
        this.tag5 = tag5;
        putTag("tag5",tag5);

    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
        putTag("appid",appid);
    }

    public String getAppScene() {
        return appScene;
    }

    public void setAppScene(String appScene) {
        this.appScene = appScene;
        putTag("appScene",appScene);
    }
}
